; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 2
; RUN: opt -disable-output "-passes=print<scalar-evolution>" < %s 2>&1 | FileCheck %s

; This test exercises the following scenario:
; given: n > 0
; for (i = 0, j = n - 1; i < n; i++, j--) {
;   a = n - i;
;   b = (n - 1) - i;
;   c = 2147483647 - 1;
; }
;
; Note that value ranges of 'i' and 'j' are the same, just inverted. It means that
; they have same ranges and same no-wrap properties. 'b' is just an alternative
; way to compute the same value as 'j'. 'a' is effectively 'j + 1' and 'c' is a
; a positive value. All involved addrecs for 'i', 'j', 'a', 'b', 'c' should have
; no-sign-wrap flag.
;
;        i's AddRec is expected to be proven no-sign-wrap
;        j's AddRec is expected to be proven no-sign-wrap
; FIXME: a's AddRec is expected to be no-sign-wrap
;        b's AddRec is expected to be no-sign-wrap
; FIXME: c's AddRec is expected to be no-sign-wrap
;        i is expected to be non-negative
;        j is expected to be non-negative
; FIXME: a is expected to be positive
; FIXME: b is expected to be non-negative
;        c is expected to be positive
define i32 @test_step_1_flags(i32 %n) {
; CHECK-LABEL: 'test_step_1_flags'
; CHECK-NEXT:  Classifying expressions for: @test_step_1_flags
; CHECK-NEXT:    %n.minus.1 = sub nsw i32 %n, 1
; CHECK-NEXT:    --> (-1 + %n) U: full-set S: full-set
; CHECK-NEXT:    %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
; CHECK-NEXT:    --> {0,+,1}<nuw><nsw><%loop> U: [0,2147483647) S: [0,2147483647) Exits: (-1 + %n) LoopDispositions: { %loop: Computable }
; CHECK-NEXT:    %j = phi i32 [ %n.minus.1, %entry ], [ %j.next, %loop ]
; CHECK-NEXT:    --> {(-1 + %n),+,-1}<nsw><%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable }
; CHECK-NEXT:    %a = sub i32 %n, %i
; CHECK-NEXT:    --> {%n,+,-1}<nw><%loop> U: full-set S: full-set Exits: 1 LoopDispositions: { %loop: Computable }
; CHECK-NEXT:    %b = sub i32 %n.minus.1, %i
; CHECK-NEXT:    --> {(-1 + %n),+,-1}<nsw><%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable }
; CHECK-NEXT:    %c = sub i32 2147483647, %i
; CHECK-NEXT:    --> {2147483647,+,-1}<nw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: (-2147483648 + (-1 * %n)) LoopDispositions: { %loop: Computable }
; CHECK-NEXT:    %i.next = add nuw nsw i32 %i, 1
; CHECK-NEXT:    --> {1,+,1}<nuw><nsw><%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: %n LoopDispositions: { %loop: Computable }
; CHECK-NEXT:    %j.next = add nsw i32 %j, -1
; CHECK-NEXT:    --> {(-2 + %n),+,-1}<nw><%loop> U: full-set S: full-set Exits: -1 LoopDispositions: { %loop: Computable }
; CHECK-NEXT:  Determining loop execution counts for: @test_step_1_flags
; CHECK-NEXT:  Loop %loop: backedge-taken count is (-1 + %n)
; CHECK-NEXT:  Loop %loop: constant max backedge-taken count is 2147483646
; CHECK-NEXT:  Loop %loop: symbolic max backedge-taken count is (-1 + %n)
; CHECK-NEXT:  Loop %loop: Predicated backedge-taken count is (-1 + %n)
; CHECK-NEXT:   Predicates:
; CHECK:       Loop %loop: Trip multiple is 1
;
entry:
  %n.minus.1 = sub nsw i32 %n, 1
  %precond = icmp sgt i32 %n, 0
  br i1 %precond, label %loop, label %fail

loop:
  %i = phi i32 [0, %entry], [%i.next, %loop]                      ; 0...n-1
  %j = phi i32 [%n.minus.1, %entry], [%j.next, %loop]             ; n-1...0
  %a = sub i32 %n, %i                                             ; n...1
  %b = sub i32 %n.minus.1, %i                                     ; n-1...0
  %c = sub i32 2147483647, %i                                     ; 2147483647...1
  %i.next = add nuw nsw i32 %i, 1
  %j.next = add nsw i32 %j, -1
  %cond = icmp slt i32 %i.next, %n
  br i1 %cond, label %loop, label %exit

fail:
  ret i32 -1

exit:
  ret i32 0
}
